﻿@model Webcar.Entitys.BS_CAR_MILE
@{ Layout = "~/Views/Shared/_leftPanelLayout.cshtml";}
@section LeftTree
{
     
    @{
        Html.RenderPartial("DeptTreePartialView", new Webcar.Models.DeptTreeMode { Action = "OilList", Controller = "Car", ShowChild = false });
     }
}
@using (Html.BeginForm("OilSave", "Car", Html.ViewContext.RouteData.Values,FormMethod.Post))
{
    @Html.AntiForgeryToken()
    @Html.HiddenFor(a => a.CAR_NO)
    <div id="edit-car" style="width:700px;margin:5px 5px 5px 5px">

        @if (ViewBag.CarOilList != null)
        { 
        @Html.DevExpress().GridView(
            settings =>
            {
                settings.Name = "gridView";
                DevControlHelper.InitDefaultPagerGridViewSettings(settings);
                settings.EnableAlternatingRowColor();
                settings.CallbackRouteValues = new { Controller = "Car", Action = "OilListPartialView", id = Html.ViewContext.RouteData.Values["id"] };
                settings.KeyFieldName = "NY";
                settings.SettingsPager.Visible = false;
                settings.SettingsPager.Mode = GridViewPagerMode.ShowAllRecords;
                settings.Columns.Add("NY", "月份");
                settings.Columns.Add("READ_MILE", "里程表读数");
                settings.Columns.Add("MILE", "本月公里数");
                settings.Columns.Add("FUEL_NUM", "实际耗油量");
                settings.Columns.Add("FUEL_RATION", "计划单耗");
                settings.Columns.Add(col =>
                {
                    col.CellStyle.Paddings.Padding = 1;
                    col.CellStyle.Paddings.PaddingLeft = 5;
                    col.Caption = "实际单耗";
                    col.Width = 70;
                    col.CellStyle.HorizontalAlign = HorizontalAlign.Right;
                    col.SetDataItemTemplateContent(c =>
                    {
                        decimal? mile = (decimal?)DataBinder.Eval(c.DataItem, "MILE");
                        decimal? fuel_num = (decimal?)DataBinder.Eval(c.DataItem, "FUEL_NUM");
                        decimal fact_ration = 0;
                        if (mile != null && mile.Value > 0)
                        {
                            fact_ration = (decimal)(100 * fuel_num) / mile.Value;
                        }
                        ViewContext.Writer.Write(
                            Math.Round(fact_ration,2).ToString()+"&nbsp"
                        );
                    }
                    );
                });

                settings.Columns.Add(col =>
                {
                    col.CellStyle.Paddings.Padding = 1;
                    col.CellStyle.Paddings.PaddingLeft = 5;
                    col.Caption = "";
                    col.Width = 80;
                    col.CellStyle.HorizontalAlign = HorizontalAlign.Center;
                    col.SetDataItemTemplateContent(c =>
                    {
                        string carno = DataBinder.Eval(c.DataItem, "CAR_NO").ToString();
                        string month = DataBinder.Eval(c.DataItem, "NY").ToString();
                        ViewContext.Writer.Write(
                            Html.ActionLink("[编辑]", "OilEdit", new { carno = carno, month = month }) + "&nbsp;"
                        );

                        ViewContext.Writer.Write(Html.ActionLink("[删除]", "OilDelete", new { car_no = carno, month = month }, new { onclick = "return confirm('是否要删除此油耗里程记录?')" }).ToHtmlString());

                    }
                    );
                });
            }).Bind(ViewBag.CarOilList).GetHtml()
        }

        <table class="edit-table">
            <tr>
                <td style="width: 80px; text-align: right;">*所属单位：</td>
                <td>@Html.DropDownList("dept_code", WebcarDataHelper.UserSuperDeptList, new { @style = "width: 100%;", @onchange = "changeDept();" })</td>
                <td style="width: 80px; text-align: right">*车辆牌照：</td>
                <td>
                    @*@Html.DropDownListFor(a => a.carno, WebcarDataHelper.GetCarByDeptList(Model.dept_Code), new { @style = "width: 100%;", @onchange = "retriveFuelMile();" })*@
                    @Html.TextBox("RECORD_NO",(string)@ViewBag.RecordNo)
                </td>
            </tr>
            <tr>
                <td style="width: 80px; text-align: right;">*月份：</td>
                <td>@Html.TextBoxFor(a => a.NY)</td>
                <td style="width: 80px; text-align: right">里程表读数：</td>
                <td>@Html.TextBoxFor(a => a.READ_MILE)</td>
            </tr>
            <tr>
                <td style="width: 80px; text-align: right;">本月公里数：</td>
                <td>@Html.TextBoxFor(a => a.MILE, new { @style = "width: 100%;"})</td>
                <td style="width: 80px; text-align: right;">实际耗油量：</td>
                <td>@Html.TextBoxFor(a => a.FUEL_NUM, new { @style = "width: 100%;"  })</td>
            </tr>
            <tr>
                <td style="width: 80px; text-align: right;">计划单耗：</td>
                <td>@Html.TextBoxFor(a => a.FUEL_RATION)</td>
                <td style="width: 80px; text-align: right">实际单耗：</td>
                <td>@Html.TextBox("fact_ration", new { @style = "width: 100%;" })</td>
            </tr>
        </table>


        <br />
        <div class="validation-errors">@ViewBag.ErrorMessage</div>

    </div>
    <table style="width:700px">
        <tr>
            <td style="width:650px"></td>
            <td>
                @Html.DevExpress().Button(settings =>
        {
            settings.Name = "Button1";
            settings.Text = "保存";
            settings.UseSubmitBehavior = true;
            settings.Width = 70;
            settings.ControlStyle.CssClass = "button1";
        }).GetHtml()
                

            </td>
            <td>
                @Html.DevExpress().Button(settings =>
                {
                    settings.ControlStyle.CssClass = "button2";
                    settings.Name = "Button2";
                    settings.Text = "取消";
                    settings.Width = 70;
                    settings.ClientSideEvents.Click = "function(s, e){ location.href='" + @Url.Action("OilList", "Car") + "' }";
                }).GetHtml()
            </td>
        </tr>

    </table>
    
 
}

   <script type="text/javascript">
    $(function () {
        calc_fuel_money();
        changeDept();
        $("#RECORD_NO").change(function () {
            $("#CAR_NO").val("");
        });

        $("#dept_code").val("@ViewBag.DeptCode");

        $("#MILE").change(function () {
            calc_fuel_money();
        });
        $("#FUEL_NUM").change(function () {
            calc_fuel_money();
        });
        $("#READ_MILE").change(function () {
            calc_fuel_mile();
        });
        $("#NY").change(function () {
            calc_fuel_mile();
        });


    })
    function changeDept() {
        setCarAutoComplete("RECORD_NO", $("#dept_code").val(),
            '@Url.Action("GetAutoCompleteCar","Common")',
            function (event, ui) {
                $("#CAR_NO").val(ui.item.key);
            }
        );
    }
    function calc_fuel_money() {
        var mile = $('#MILE').val();
        var fuel_num = $('#FUEL_NUM').val();
        if (mile > 0 && fuel_num > 0) {
            var fact = (fuel_num * 100) / mile;
            $('#fact_ration').val(fact.toFixed(2));
        }
        else {
            $('#fact_ration').val(0);
        }
    }
    function calc_fuel_mile() {
        var action = '@Url.Action("RetrieveCarMile","Common")';
        
        var selectCar = $('#CAR_NO').val();
        var ny = $('#NY').val();
        $.get(action, { car_no: selectCar, month: ny }, function (data) {
            var mile = $('#READ_MILE').val() - data;
            $('#MILE').val(mile);
        });
    }
</script>    